package com.zs.toolbox.web2j.system.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.system.core.entity.WtDictionaryItemEntity;
import com.zs.toolbox.web2j.system.core.entity.WtDictionaryTypeEntity;
import com.zs.toolbox.web2j.system.core.mapper.other.WtDictionaryItemMapper;
import com.zs.toolbox.web2j.system.core.service.WtDictionaryItemService;
import com.zs.toolbox.web2j.system.core.service.WtDictionaryTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 字典类型条目表 服务实现类
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-08
 */
@Service
public class WtDictionaryItemServiceImpl extends ServiceImpl<WtDictionaryItemMapper, WtDictionaryItemEntity> implements WtDictionaryItemService {
    @Autowired
    WtDictionaryTypeService wtDictionaryTypeService;


    @Override
    public boolean saveOrUpdate(WtDictionaryItemEntity entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(UUID.randomUUID().toString());
        }

        String typeId = entity.getTypeId();
        WtDictionaryTypeEntity wtDictionaryTypeEntity = wtDictionaryTypeService.getById(typeId);
        if(null == wtDictionaryTypeEntity) {
            throw WtExceptions.E_DICT_KEY;
        }
        return super.saveOrUpdate(entity);
    }

    @Override
    public List<WtDictionaryItemEntity> listByTypeId(String typeId) {
        return this.list(new LambdaQueryWrapper<WtDictionaryItemEntity>()
                .eq(WtDictionaryItemEntity::getTypeId, typeId));
    }

}
